
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rechargeIndex: '',
    recharge: [
      {
        priceX: '30元',
        priceS: '售价29.34',
        loading: false,
        index: 0
      },
      {
        priceX: '50元',
        priceS: '售价29.34',
        loading: false,
        index: 1
      },
      {
        priceX: '50元',
        priceS: '售价29.34',
        loading: false,
        index: 2
      },
      {
        priceX: '80元',
        priceS: '售价89.34',
        loading: false,
        index: 3
      },
      {
        priceX: '90元',
        priceS: '售价99.34',
        loading: false,
        index: 4
      },
      {
        priceX: '100元',
        priceS: '售价129.34',
        loading: false,
        index: 5
      },
    ],
    hideHead: false,
    rechargeOnoff: false,
    historys: true,
    inpuVal: "",//input框内值
    listarr: [],//创建数组
    keydown_number: 0,//检测input框内是否有内容
    input_value: "",//value值
    hostarr: [],//接收请求存储数组  
    listarrShow: [],
    name_focus: false,//获取焦点
    rechargeCardRadio: [//有卡无卡
      {
        "title": '有卡充值',
        'index': '0'
      },
      {
        "title": '无卡充值',
        'index': '1'
      },
    ],
    rechargeCardSelect: 0,//单选默认
    springboxHide: true,//弹框隐藏
    springboxLoading: false,
    springboxCard: '',//手机号卡号
  },
  /**
 * 生命周期函数--监听页面加载
 */
  onLoad: function (options) {
    let This = this;
    //读取缓存历史搜索记录
    wx.getStorage({
      key: 'list_arr',
      success: function (res) {
        This.setData({
          listarr: res.data
        })
      }
    })
  },
  hideHead() {
    this.setData({
      hideHead: true
    })
  },
  goRecharge(e) {//跳转到首页
    var that = this
    var rechargeIndex = that.data.rechargeIndex;
    var recharge = that.data.recharge
    var index = e.currentTarget.dataset.index;
    recharge[index].loading = true
    that.setData({
      rechargeIndex: rechargeIndex,
      recharge: recharge
    });
    setTimeout(function () {
      recharge[index].loading = false
      that.setData({
        rechargeIndex: rechargeIndex,
        recharge: recharge
      });
    }, 1000)
  },
  //取值input判断输入框内容修改按钮
  inputvalue: function (e) {
    const value = e.detail.value;
    this.setData({
      inputVal: e.detail.value,
      springboxCard: e.detail.value,
    })
    if (value) {
      this.setData({
        rechargeOnoff: true
      })

    } else {
      this.setData({
        rechargeOnoff: false
      })
    }
    if (e.detail.value == '') {
      this.setData({
        historys: true
      })
    } else {
      if (this.data.listarr.length == 0) {
        this.setData({
          historys: true
        })
      } else {
        this.setData({
          historys: false
        })
      }
    }
    var listarrShow = [];//判断是否有相同历史记录
    var listarr = this.data.listarr;
    for (var o = 0; o < listarr.length; o++) {
      if (listarr[o].indexOf(value) >= 0) {
        listarrShow.unshift(listarr[o]);
        console.log(listarrShow)
      } else {
        console.log(listarrShow)
      }
      this.setData({
        listarrShow: listarrShow
      });
    };

  },
  oo:function(e){
    const value = e.detail.value;
  },
  //搜索方法
  search: function () {
    var springboxCard = this.data.springboxCard
    this.setData({
      input_value: springboxCard,//20200701
    })
    let This = this;
    //把获取的input值插入数组里面
    let arr = this.data.listarr;
    // console.log(this.data.inputVal)
    console.log(this.data.input_value)
    //判断取值是手动输入还是点击赋值
    if (this.data.input_value == "") {
      // console.log('进来第er个')
      // 判断数组中是否已存在
      let arrnum = arr.indexOf(this.data.inputVal);
      console.log(arr.indexOf(this.data.inputVal));
      if (arrnum != -1) {
        // 删除已存在后重新插入至数组
        arr.splice(arrnum, 1)
        arr.unshift(this.data.inputVal);

      } else {
        arr.unshift(this.data.inputVal);
      }
    } else {
      console.log('进来第一个')
      let arr_num = arr.indexOf(this.data.input_value);
      console.log(arr.indexOf(this.data.input_value));
      if (arr_num != -1) {
        arr.splice(arr_num, 1)
        arr.unshift(this.data.input_value);
      } else {
        arr.unshift(this.data.input_value);
      }
      this.setData({
        input_value: this.data.inpuVal,
      })
    }
    console.log(arr)
    //存储搜索记录
    wx.setStorage({
      key: "list_arr",
      data: arr
    })
    //取出搜索记录
    wx.getStorage({
      key: 'list_arr',
      success: function (res) {
        This.setData({
          listarr: res.data
        })
      }
    })
    this.setData({
      springboxHide: true,
      springboxLoading: true,
      rechargeOnoff:false,
    })
    var that = this
    setTimeout(function () {
      that.setData({
        historys: true,
      });
    }, 50)
    setTimeout(function () {
      that.setData({
        springboxLoading: false,
      });
    }, 1000)
  },
  //清除搜索记录
  delete_list: function () {
    //清除当前数据
    this.setData({
      listarr: [],
      historys: true
    });
    //清除缓存数据
    wx.removeStorage({
      key: 'list_arr'
    })
  },
  //点击赋值到input框
  this_value: function (e) {
    let value = e.currentTarget.dataset.text;
    this.setData({
      inputVal:value,
      input_value:value,
      springboxCard:value,
      name_focus:false,
    });
    var that = this
    setTimeout(function () {
      that.setData({
        inputVal: value,//20200701
        input_value: value,
        springboxCard: value,       
        historys: true,
      });
    }, 100);
  },
  //有卡无卡充值单选 
  rechargeCard: function (e) {
    let value = e.detail.value;
    this.setData({
      rechargeCardSelect: value,
    });
  },
  springboxBtn: function () {
    var inputVal = this.data.inputVal;

    this.setData({
      springboxHide: false,
      // input_value: inputVal20200701
    })

  },
  springboxHide: function () {
    this.setData({
      springboxHide: true,
    })
  }
})
